package com.tutou.ikuser.service.impl;

import com.tutou.ikuser.mapper.UserMapper;
import com.tutou.ikuser.pojo.po.SecurityUser;
import com.tutou.ikuser.pojo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        // 查询用户
        User user = userMapper.selectUser(phone);

        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户不存在~");
        }
        // 查询权限信息

        //把数据分装成UserDetails返回
        return new SecurityUser(user);
    }
}
